﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyShopThoiTrang.QuanLyShopThoiTrang_BUS_WS;

namespace QuanLyShopThoiTrang
{
    public partial class QuanLyHangHoa : Form
    {
        int iIndex = 0;
        int iIndex2 = 0;
        int iIndex3 = 0;

        bool isEditing = false;
        bool isEditing2 = false;
        bool isEditing3 = false;

        SanPham temp = new SanPham();
        SanPham temp2 = new SanPham();
        LoaiSanPham temp3 = new LoaiSanPham();

        QLSTT_BUSSoapClient ws = new QLSTT_BUSSoapClient();

        public QuanLyHangHoa()
        {
            InitializeComponent();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void QuanLyHangHoa_Load(object sender, EventArgs e)
        {
            //FillDatagrid1();
        }

        private void button12_Click(object sender, EventArgs e)
        {
            ThemLoaiHang tlh = new ThemLoaiHang();
            tlh.Show();
            tlh.Set(this);
        }

        private void button10_Click(object sender, EventArgs e)
        {
            LoaiSanPham lsp = (LoaiSanPham)dataGridView3.Rows[dataGridView3.SelectedCells[0].RowIndex].Tag;
            if(ws.LoaiSanPham_Xoa(lsp))
            {
                MessageBox.Show("Đã xóa thành công.");
            }
            else
            {
                MessageBox.Show("Xóa thất bại");
            }
            FillDatagrid3();
        }

        public void FillDatagrid3()
        {
            dataGridView3.Rows.Clear();
            int i = 1;
            List<LoaiSanPham> listLoaiSP = ws.LoaiSanPham_LayDanhSach();
            foreach (LoaiSanPham lsp in listLoaiSP)
            {
                    dataGridView3.Rows.Add(i++, lsp.LoaiSanPham_Ten);
                    dataGridView3.Rows[i - 2].Tag = lsp;
            }
        }

        public void FillDatagrid1()
        {
            dataGridView1.Rows.Clear();
            int i = 1;
            SanPham spx = new SanPham();
            spx.MaSanPham = -1;
            List<SanPham> listSP = ws.SanPham_TraCuu(spx);
            foreach (SanPham sp in listSP)
            {
                LoaiSanPham lsp = ws.SanPham_LayLoaiSP(sp);
                NhaCungCap ncc = ws.SanPham_LayNCC(sp);
                dataGridView1.Rows.Add(i++, sp.SanPham_MaHang, sp.SanPham_Ten, lsp.LoaiSanPham_Ten, ncc.NhaCungCap_Ten, sp.SanPham_NhanHieu, sp.SanPham_XuatXu, sp.SanPham_KichCo, sp.SanPham_SoLuong, sp.SanPham_GiaKhiMua, sp.SanPham_GiaKhiBan, sp.SanPham_GiaUuDai, sp.SanPham_HinhAnh);
                dataGridView1.Rows[i - 2].Tag = sp;
            }
            List<LoaiSanPham> listLoaiSP = ws.LoaiSanPham_LayDanhSach();
            comboBox1.DataSource = listLoaiSP;
            comboBox1.DisplayMember = "LoaiSanPham_Ten";

            NhaCungCap temp = new NhaCungCap();
            temp.MaNhaCungCap = -1;
            List<NhaCungCap> listNCC = ws.NhaCungCap_TraCuu(temp);

            comboBox2.DataSource = listNCC;
            comboBox2.DisplayMember = "NhaCungCap_Ten";
        }

        public void FillDatagrid2()
        {
            dataGridView2.Rows.Clear();
            int i = 1;
            SanPham spx = new SanPham();
            spx.MaSanPham = -2;
            List<SanPham> listSP = ws.SanPham_TraCuu(spx);
            foreach (SanPham sp in listSP)
            {
                LoaiSanPham lsp = ws.SanPham_LayLoaiSP(sp);
                NhaCungCap ncc = ws.SanPham_LayNCC(sp);
                dataGridView2.Rows.Add(i++, sp.SanPham_MaHang, sp.SanPham_Ten, lsp.LoaiSanPham_Ten, ncc.NhaCungCap_Ten, sp.SanPham_NhanHieu, sp.SanPham_XuatXu, sp.SanPham_KichCo, sp.SanPham_SoLuong, sp.SanPham_GiaKhiMua, sp.SanPham_GiaKhiBan, sp.SanPham_GiaUuDai, sp.SanPham_HinhAnh);
                dataGridView2.Rows[i - 2].Tag = sp;
            }
            List<LoaiSanPham> listLoaiSP = ws.LoaiSanPham_LayDanhSach();
            comboBox4.DataSource = listLoaiSP;
            comboBox4.DisplayMember = "LoaiSanPham_Ten";

            NhaCungCap temp = new NhaCungCap();
            temp.MaNhaCungCap = -1;
            List<NhaCungCap> listNCC = ws.NhaCungCap_TraCuu(temp);

            comboBox3.DataSource = listNCC;
            comboBox3.DisplayMember = "NhaCungCap_Ten";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (isEditing)
            {
                if (textBox2.Text == "")
                {
                    MessageBox.Show("Không được để trống tên hàng");
                    return;
                }

                if (textBox6.Text == "")
                {
                    MessageBox.Show("Không được để trống giá bán");
                    return;
                }

                if (textBox9.Text == "")
                {
                    MessageBox.Show("Không được để trống số lượng");
                    return;
                }

                

                int masp = temp.MaSanPham;
                string hinhAnh = temp.SanPham_HinhAnh;
                string ma = temp.SanPham_MaHang;
                temp = new SanPham();
                temp.SanPham_HinhAnh = hinhAnh;
                temp.SanPham_MaHang = ma;
                temp.MaSanPham = masp;

                temp.SanPham_Ten = textBox2.Text;
                temp.SanPham_NhanHieu = textBox3.Text;
                temp.SanPham_XuatXu = textBox4.Text;
                if (ws.KiemTraDouble(textBox5.Text))
                    temp.SanPham_GiaKhiMua = double.Parse(textBox5.Text);
                else
                {
                    MessageBox.Show("Giá mua không hợp lệ");
                    return;
                }
                if (ws.KiemTraDouble(textBox6.Text))
                    temp.SanPham_GiaKhiBan = double.Parse(textBox6.Text);
                else
                {
                    MessageBox.Show("Giá bán không hợp lệ");
                    return;
                }
                if (textBox7.Text != "")
                {
                    if (ws.KiemTraDouble(textBox7.Text))
                        temp.SanPham_GiaUuDai = double.Parse(textBox7.Text);
                    else
                    {
                        MessageBox.Show("Giá ưu đãi không hợp lệ");
                        return;
                    }
                }
                temp.SanPham_KichCo = textBox8.Text;
                if (ws.KiemTraInt(textBox9.Text))
                    temp.SanPham_SoLuong = int.Parse(textBox9.Text);
                else
                {
                    MessageBox.Show("Số lượng không hợp lệ");
                    return;
                }
                temp.SanPham_TrangThai = "Su dung";


                LoaiSanPham lsp = (LoaiSanPham)comboBox1.SelectedItem;
                temp.MaLoaiSanPham = lsp.MaLoaiSanPham;

                NhaCungCap ncc = (NhaCungCap)comboBox2.SelectedItem;
                temp.MaNhaCungCap = ncc.MaNhaCungCap;

                dataGridView1.Rows[iIndex].Tag = temp;
                dataGridView1.Rows[iIndex].SetValues(iIndex + 1, temp.SanPham_MaHang, temp.SanPham_Ten, comboBox1.Text, comboBox2.Text, temp.SanPham_NhanHieu, temp.SanPham_XuatXu, temp.SanPham_KichCo, temp.SanPham_SoLuong, temp.SanPham_GiaKhiMua, temp.SanPham_GiaKhiBan, temp.SanPham_GiaUuDai, temp.SanPham_HinhAnh);
                if(ws.SanPham_CapNhat(temp))
                {
                    MessageBox.Show("Thành công");
                }
                else
                {
                    MessageBox.Show("Thất bại");
                }


                textBox2.ReadOnly = true;
                textBox3.ReadOnly = true;
                textBox4.ReadOnly = true;
                //textBox5.ReadOnly = false;
                textBox6.ReadOnly = true;
                textBox7.ReadOnly = true;
                textBox8.ReadOnly = true;
                textBox9.ReadOnly = true;

                dataGridView1.Enabled = true;
                button2.Enabled = true;
                button8.Enabled = true;
                button5.Enabled = true;
                button4.Enabled = true;

                comboBox1.Enabled = false;
                comboBox2.Enabled = false;
                isEditing = false;
            }
            else
            {
                if (dataGridView1.SelectedCells.Count == 0)
                {
                    MessageBox.Show("Hãy chọn một sản phẩm để thay đổi");
                    return;
                }
                DataGridViewRow row = dataGridView1.Rows[dataGridView1.SelectedCells[0].RowIndex];

                temp = (SanPham)row.Tag;
                iIndex = row.Index;

                dataGridView1.Enabled = false;
                button2.Enabled = false;
                button8.Enabled = false;
                button5.Enabled = false;
                button4.Enabled = false;

                //textBox1.ReadOnly = false;
                textBox2.ReadOnly = false;
                textBox3.ReadOnly = false;
                textBox4.ReadOnly = false;
                //textBox5.ReadOnly = false;
                textBox6.ReadOnly = false;
                textBox7.ReadOnly = false;
                textBox8.ReadOnly = false;
                textBox9.ReadOnly = false;

                comboBox1.Enabled = true;
                comboBox2.Enabled = true;

                isEditing = true;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            NhapHang nh = new NhapHang();
            nh.Show();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Bạn muốn xóa những sản phẩm đã chọn?", "Xác nhận", MessageBoxButtons.YesNoCancel);
            if (dr == DialogResult.Yes)
            {
                foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
                {
                    DataGridViewRow row = dataGridView1.Rows[cell.RowIndex];

                    SanPham sp = (SanPham)row.Tag;

                    ws.SanPham_Xoa(sp);

                    dataGridView1.Rows.RemoveAt(cell.RowIndex);
                }

                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    row.SetValues(row.Index + 1);
                }
            }
            else
            {
                return;
            }
            
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex == 2)
            {
                FillDatagrid3();
                List<LoaiSanPham> listLoaiSP = ws.LoaiSanPham_LayDanhSach();
                comboBox1.DataSource = listLoaiSP;
                comboBox1.DisplayMember = "LoaiSanPham_Ten";

                NhaCungCap temp = new NhaCungCap();
                temp.MaNhaCungCap = -1;
                List<NhaCungCap> listNCC = ws.NhaCungCap_TraCuu(temp);

                comboBox2.DataSource = listNCC;
                comboBox2.DisplayMember = "NhaCungCap_Ten";
                return;
            }
            /*if(tabControl1.SelectedIndex == 1)
            {
                FillDatagrid2();
                
                return;
            }
            if (tabControl1.SelectedIndex == 0)
            {
                FillDatagrid1();
                
                return;
            }*/
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (isEditing2)
            {
                if (textBox17.Text == "")
                {
                    MessageBox.Show("Không được để trống tên hàng");
                    return;
                }

                if (textBox13.Text == "")
                {
                    MessageBox.Show("Không được để trống giá bán");
                    return;
                }

                if (textBox10.Text == "")
                {
                    MessageBox.Show("Không được để trống số lượng");
                    return;
                }

                dataGridView2.Enabled = true;
                button9.Enabled = true;
                button1.Enabled = true;
                button6.Enabled = true;

                int masp = temp2.MaSanPham;
                string hinhAnh = temp2.SanPham_HinhAnh;
                string ma = temp2.SanPham_MaHang;
                temp2 = new SanPham();
                temp2.SanPham_HinhAnh = hinhAnh;
                temp2.SanPham_MaHang = ma;
                temp2.MaSanPham = masp;

                temp2.SanPham_Ten = textBox17.Text;
                temp2.SanPham_NhanHieu = textBox16.Text;
                temp2.SanPham_XuatXu = textBox15.Text;
                if(ws.KiemTraDouble(textBox14.Text))
                    temp2.SanPham_GiaKhiMua = double.Parse(textBox14.Text);
                else
                {
                    MessageBox.Show("Giá mua không hợp lệ");
                    return;
                }
                if (ws.KiemTraDouble(textBox13.Text))
                    temp2.SanPham_GiaKhiBan= double.Parse(textBox13.Text);
                else
                {
                    MessageBox.Show("Giá bán không hợp lệ");
                    return;
                }
                if (textBox12.Text != "")
                {
                    if (ws.KiemTraDouble(textBox12.Text))
                        temp2.SanPham_GiaUuDai = double.Parse(textBox12.Text);
                    else
                    {
                        MessageBox.Show("Giá ưu đãi không hợp lệ");
                        return;
                    }
                }
                temp2.SanPham_KichCo = textBox11.Text;
                if(ws.KiemTraInt(textBox10.Text))
                    temp2.SanPham_SoLuong = int.Parse(textBox10.Text);
                else
                {
                    MessageBox.Show("Số lượng không hợp lệ");
                    return;
                }
                temp2.SanPham_TrangThai = "Su dung";


                LoaiSanPham lsp = (LoaiSanPham)comboBox4.SelectedItem;
                temp2.MaLoaiSanPham = lsp.MaLoaiSanPham;

                NhaCungCap ncc = (NhaCungCap)comboBox3.SelectedItem;
                temp2.MaNhaCungCap = ncc.MaNhaCungCap;

                dataGridView2.Rows[iIndex2].Tag = temp2;
                dataGridView2.Rows[iIndex2].SetValues(iIndex2 + 1, temp2.SanPham_MaHang, temp2.SanPham_Ten, comboBox4.Text, comboBox3.Text, temp2.SanPham_NhanHieu, temp2.SanPham_XuatXu, temp2.SanPham_KichCo, temp2.SanPham_SoLuong, temp2.SanPham_GiaKhiMua, temp2.SanPham_GiaKhiBan, temp2.SanPham_GiaUuDai, temp2.SanPham_HinhAnh);
                if (ws.SanPham_CapNhat(temp2))
                {
                    MessageBox.Show("Thành công");
                }
                else
                {
                    MessageBox.Show("Thất bại");
                }


                textBox17.ReadOnly = true;
                textBox16.ReadOnly = true;
                textBox15.ReadOnly = true;
                //textBox14.ReadOnly = false;
                textBox13.ReadOnly = true;
                textBox12.ReadOnly = true;
                textBox11.ReadOnly = true;
                textBox10.ReadOnly = true;

                comboBox4.Enabled = false;
                comboBox3.Enabled = false;
                isEditing2 = false;
            }
            else
            {
                if (dataGridView2.SelectedCells.Count == 0)
                {
                    MessageBox.Show("Hãy chọn một sản phẩm để thay đổi");
                    return;
                }
                DataGridViewRow row = dataGridView2.Rows[dataGridView2.SelectedCells[0].RowIndex];

                temp2 = (SanPham)row.Tag;
                iIndex2 = row.Index;

                dataGridView2.Enabled = false;
                button9.Enabled = false;
                button1.Enabled = false;
                button6.Enabled = false;

                //textBox1.ReadOnly = false;
                textBox17.ReadOnly = false;
                textBox16.ReadOnly = false;
                textBox15.ReadOnly = false;
                //textBox14.ReadOnly = false;
                textBox13.ReadOnly = false;
                textBox12.ReadOnly = false;
                textBox11.ReadOnly = false;
                textBox10.ReadOnly = false;

                dataGridView2.Enabled = true;
                button9.Enabled = true;
                button1.Enabled = true;
                button6.Enabled = true;

                comboBox4.Enabled = true;
                comboBox3.Enabled = true;

                isEditing2 = true;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Bạn muốn xóa những sản phẩm đã chọn?", "Xác nhận", MessageBoxButtons.YesNoCancel);
            if (dr == DialogResult.Yes)
            {
                foreach (DataGridViewCell cell in dataGridView2.SelectedCells)
                {
                    DataGridViewRow row = dataGridView2.Rows[cell.RowIndex];

                    SanPham sp = (SanPham)row.Tag;

                    ws.SanPham_Xoa(sp);

                    dataGridView2.Rows.RemoveAt(cell.RowIndex);
                }

                foreach (DataGridViewRow row in dataGridView2.Rows)
                {
                    row.SetValues(row.Index + 1);
                }
            }
            else
            {
                return;
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            NhapHang nh = new NhapHang();
            nh.Show();
        }

        private void button11_Click(object sender, EventArgs e)
        {
            if (isEditing3)
            {
                if (textBox19.Text == "")
                {
                    MessageBox.Show("Không được để trống tên loại");
                    return;
                }


                dataGridView3.Enabled = true;
                button10.Enabled = true;
                button12.Enabled = true;

                int ma = temp3.MaLoaiSanPham;
                temp3 = new LoaiSanPham();
                temp3.MaLoaiSanPham = ma;

                temp3.LoaiSanPham_Ten = textBox19.Text;
                temp3.LoaiSanPham_TrangThai = "Su dung";

                dataGridView3.Rows[iIndex3].Tag = temp3;
                dataGridView3.Rows[iIndex3].SetValues(iIndex3 + 1, temp3.LoaiSanPham_Ten);
                if (ws.LoaiSanPham_CapNhat(temp3))
                {
                    MessageBox.Show("Thành công");
                }
                else
                {
                    MessageBox.Show("Thất bại");
                }


                textBox19.ReadOnly = true;
                isEditing3 = false;
            }
            else
            {
                if (dataGridView3.SelectedCells.Count == 0)
                {
                    MessageBox.Show("Hãy chọn một loại sản phẩm để thay đổi");
                    return;
                }
                DataGridViewRow row = dataGridView3.Rows[dataGridView3.SelectedCells[0].RowIndex];

                temp3 = (LoaiSanPham)row.Tag;
                iIndex3 = row.Index;

                dataGridView3.Enabled = false;
                button10.Enabled = false;
                button12.Enabled = false;

                textBox19.ReadOnly = false;

                isEditing3 = true;
            }
        }

        private void dataGridView3_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow row = dataGridView3.Rows[dataGridView3.SelectedCells[0].RowIndex];

            LoaiSanPham lsp = (LoaiSanPham)row.Tag;

            textBox19.Text = lsp.LoaiSanPham_Ten;
        }

        private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow row = dataGridView2.Rows[dataGridView2.SelectedCells[0].RowIndex];

            SanPham sp = (SanPham)row.Tag;

            try
            {
                Image itemp = Image.FromFile(sp.SanPham_HinhAnh);
                Graphics g = pictureBox2.CreateGraphics();
                g.DrawImage(itemp, new Rectangle(0, 0, 110, 110), new Rectangle(0, 0, itemp.Width, itemp.Height), GraphicsUnit.Pixel);
                g.Dispose();
            }
            catch (System.Exception ex)
            {
                pictureBox2.Image = Properties.Resources.no_image;
            }

            textBox18.Text = sp.SanPham_MaHang;
            textBox17.Text = sp.SanPham_Ten;
            textBox16.Text = (sp.SanPham_NhanHieu != null) ? sp.SanPham_NhanHieu : "";
            textBox15.Text = (sp.SanPham_XuatXu != null) ? sp.SanPham_XuatXu : "";
            textBox14.Text = sp.SanPham_GiaKhiMua.ToString();
            textBox13.Text = sp.SanPham_GiaKhiBan.ToString();
            textBox12.Text = (sp.SanPham_GiaUuDai != null) ? sp.SanPham_GiaUuDai.ToString() : "";
            textBox11.Text = (sp.SanPham_KichCo != null) ? sp.SanPham_KichCo : "";
            textBox10.Text = sp.SanPham_SoLuong.ToString();

            comboBox4.Enabled = true;
            foreach (LoaiSanPham lsp in comboBox4.Items)
            {
                if (lsp.MaLoaiSanPham == sp.MaLoaiSanPham)
                {
                    comboBox4.SelectedItem = lsp;
                }
            }
            comboBox4.Enabled = false;

            comboBox3.Enabled = true;
            foreach (NhaCungCap ncc in comboBox3.Items)
            {
                if (ncc.MaNhaCungCap == sp.MaNhaCungCap)
                {
                    comboBox3.SelectedItem = ncc;
                }
            }
            comboBox3.Enabled = false;
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow row = dataGridView1.Rows[dataGridView1.SelectedCells[0].RowIndex];

            SanPham sp = (SanPham)row.Tag;

            try
            {
                Image temp = Image.FromFile(sp.SanPham_HinhAnh);
                Graphics g = pictureBox1.CreateGraphics();
                g.DrawImage(temp, new Rectangle(0, 0, 110, 110), new Rectangle(0, 0, temp.Width, temp.Height), GraphicsUnit.Pixel);
                g.Dispose();
            }
            catch (System.Exception ex)
            {
                pictureBox1.Image = Properties.Resources.no_image;
            }

            textBox1.Text = sp.SanPham_MaHang;
            textBox2.Text = sp.SanPham_Ten;
            textBox3.Text = (sp.SanPham_NhanHieu != null) ? sp.SanPham_NhanHieu : "";
            textBox4.Text = (sp.SanPham_XuatXu != null) ? sp.SanPham_XuatXu : "";
            textBox5.Text = sp.SanPham_GiaKhiMua.ToString();
            textBox6.Text = sp.SanPham_GiaKhiBan.ToString();
            textBox7.Text = (sp.SanPham_GiaUuDai != null) ? sp.SanPham_GiaUuDai.ToString() : "";
            textBox8.Text = (sp.SanPham_KichCo != null) ? sp.SanPham_KichCo : "";
            textBox9.Text = sp.SanPham_SoLuong.ToString();

            comboBox1.Enabled = true;
            foreach (LoaiSanPham lsp in comboBox1.Items)
            {
                if (lsp.MaLoaiSanPham == sp.MaLoaiSanPham)
                {
                    comboBox1.SelectedItem = lsp;
                }
            }
            comboBox1.Enabled = false;

            comboBox2.Enabled = true;
            foreach (NhaCungCap ncc in comboBox2.Items)
            {
                if (ncc.MaNhaCungCap == sp.MaNhaCungCap)
                {
                    comboBox2.SelectedItem = ncc;
                }
            }
            comboBox2.Enabled = false;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            TraCuuSanPham tchh = new TraCuuSanPham();
            tchh.Set(this);
            tchh.ShowDialog();
        }

        public void TraCuu(SanPham spx)
        {
            if (spx.SanPham_MaHang != "")
            {
                SanPham sp = ws.SanPham_LaySP(spx);
                if (sp != null)
                {
                    dataGridView1.Rows.Clear();
                    {
                        LoaiSanPham lsp = ws.SanPham_LayLoaiSP(sp);
                        NhaCungCap ncc = ws.SanPham_LayNCC(sp);
                        dataGridView1.Rows.Add(1, sp.SanPham_MaHang, sp.SanPham_Ten, lsp.LoaiSanPham_Ten, ncc.NhaCungCap_Ten, sp.SanPham_NhanHieu, sp.SanPham_XuatXu, sp.SanPham_KichCo, sp.SanPham_SoLuong, sp.SanPham_GiaKhiMua, sp.SanPham_GiaKhiBan, sp.SanPham_GiaUuDai, sp.SanPham_HinhAnh);
                        dataGridView1.Rows[0].Tag = sp;
                    }
                }
            }
            else
            {
                List<SanPham> listSP = ws.SanPham_TraCuu(spx);
                dataGridView1.Rows.Clear();
                int i = 1;
                foreach (SanPham sp in listSP)
                {
                    LoaiSanPham lsp = ws.SanPham_LayLoaiSP(sp);
                    NhaCungCap ncc = ws.SanPham_LayNCC(sp);
                    dataGridView1.Rows.Add(i++, sp.SanPham_MaHang, sp.SanPham_Ten, lsp.LoaiSanPham_Ten, ncc.NhaCungCap_Ten, sp.SanPham_NhanHieu, sp.SanPham_XuatXu, sp.SanPham_KichCo, sp.SanPham_SoLuong, sp.SanPham_GiaKhiMua, sp.SanPham_GiaKhiBan, sp.SanPham_GiaUuDai, sp.SanPham_HinhAnh);
                    dataGridView1.Rows[i - 2].Tag = sp;
                }
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            if (isEditing)
            {
                OpenFileDialog open = new OpenFileDialog();
                open.Filter = "JPG Files|*.jpg";
                open.Title = "Chọn hình ảnh để thay đổi";
                open.InitialDirectory = Environment.CurrentDirectory;
                if (open.ShowDialog() == DialogResult.OK)
                {
                    temp.SanPham_HinhAnh = open.FileName;
                }

                Image itemp = Image.FromFile(temp.SanPham_HinhAnh);
                Graphics g = pictureBox1.CreateGraphics();
                g.DrawImage(itemp, new Rectangle(0, 0, 110, 110), new Rectangle(0, 0, itemp.Width, itemp.Height), GraphicsUnit.Pixel);
                g.Dispose();
            }
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            if (isEditing2)
            {
                OpenFileDialog open = new OpenFileDialog();
                open.Filter = "JPG Files|*.jpg";
                open.Title = "Chọn hình ảnh để thay đổi";
                open.InitialDirectory = Environment.CurrentDirectory;
                if (open.ShowDialog() == DialogResult.OK)
                {
                    temp2.SanPham_HinhAnh = open.FileName;
                }

                Image itemp = Image.FromFile(temp2.SanPham_HinhAnh);
                Graphics g = pictureBox2.CreateGraphics();
                g.DrawImage(itemp, new Rectangle(0, 0, 110, 110), new Rectangle(0, 0, itemp.Width, itemp.Height), GraphicsUnit.Pixel);
                g.Dispose();
            }
        }

        private void button4_Click_1(object sender, EventArgs e)
        {
            FillDatagrid1();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            FillDatagrid2();
        }
    }
}
